package com.lots.controller.user;

import com.lots.context.BaseContext;
import com.lots.domain.dto.FavoriteDto;
import com.lots.domain.dto.LoginDto;
import com.lots.domain.dto.RegisterDto;
import com.lots.domain.entity.Dish;
import com.lots.utils.Result;
import com.lots.domain.entity.User;
import com.lots.domain.vo.LoginInfo;
import com.lots.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user")
@Tag(name = "用户模块相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result login(@RequestBody LoginDto loginDto) {
        log.info("用户登录");
       LoginInfo loginInfo = userService.login(loginDto);
       if(loginInfo==null){
           return Result.error("用户名或密码错误");
       }
        return Result.success(loginInfo);
    }
    /**
     * 用户获取验证码
     */
    @GetMapping("/code")
    @Operation(summary = "用户获取验证码")
    public Result code(@RequestParam("phone") String  phone) {
        userService.code(phone);
        return Result.success("验证码已发送");
    }


    /**
     * 用户注册
     */
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public Result register(@RequestBody RegisterDto registerDto) {
     Integer result =  userService.register(registerDto);
     if(result==1){
         return Result.success("注册成功！");
     }else if(result == 0) {
         return Result.error("验证码错误！");
     }else if(result== -2 ){
         return Result.error("验证码过期！");
     }
     else{
         return Result.error("用户已存在！");
     }
    }
    /**
     * 用户添加/取消收藏菜品
     */
    @PostMapping("/favorite/{dishId}")
    @Operation(summary = "用户收藏菜品")
    public Result favorite(@PathVariable Long dishId) {
        FavoriteDto favoriteDto = new FavoriteDto();
        favoriteDto.setDishId(dishId);
        Long userId = BaseContext.getCurrentId();
        favoriteDto.setUserId(userId);
        userService.favorite(favoriteDto);
        return Result.success("成功");
    }
    /**
     * 查询用户作品列表
     */
    @GetMapping("/works")
    @Operation(summary = "查询用户作品列表")
    public Result works() {
     List<Dish> dish =userService.queryWorks();
        return Result.success(dish);
    }
    /**
     * 查询用户收藏菜品列表
     */
    @GetMapping("/stars")
    @Operation(summary = "查询用户收藏菜品列表")
    public Result stars() {
    List<Dish> dish = userService.queryStars();
        return Result.success(dish);
    }
    /**
     * 修改用户头像
     */
    @PostMapping("/updateAvatar")
    @Operation(summary = "修改用户头像")
    public Result updateAvatar(@RequestParam("avatar") String avatar) {
        Long userId = BaseContext.getCurrentId();
        userService.updateAvatar(userId,avatar);
        return Result.success("修改成功！");
    }
    /**
     * 修改用户昵称
     */
    @PostMapping("/updateUserName")
    @Operation(summary = "修改用户昵称")
    public Result updateNickname(@RequestParam("userName") String name) {
        Long userId = BaseContext.getCurrentId();
        userService.updateUserName(userId,name);
        return Result.success("修改成功！");
    }




}
